package week04;

import org.omg.Messaging.SYNC_WITH_TRANSPORT;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lv
 * @date 2021/4/10 12:04
 */
public class ThreadTest {
    public static void main(String[] args) throws InterruptedException {
        //threadBuild1();
        //threadBuild2();
        //threadBuild3();
        //threadBuild4();
        //threadBuild5();
        threadBuild6();
    }
    private static void threadBuild6() throws InterruptedException {

        Long start = System.currentTimeMillis();
        AtomicInteger atomicInteger = new AtomicInteger();
        Thread t = new Thread(()->{
            //System.out.println(sum()); //这是得到的返回值
            atomicInteger.set(sum());
        });
        t.start();
        t.join();
        System.out.println(atomicInteger.get());;
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
    }
    private static void threadBuild5() throws InterruptedException {
        Long start = System.currentTimeMillis();
        AtomicInteger atomicInteger = new AtomicInteger();
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                atomicInteger.set(sum());
            }
        });
        Thread.sleep(1000);
        System.out.println("异步计算结果为："+atomicInteger.get());
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
    }
    private static void threadBuild4() throws InterruptedException {
        Long start = System.currentTimeMillis();
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future future = executorService.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName());
                return sum();
            }
        });
        try {
            System.out.println("异步计算结果为："+future.get());
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
    }
    private static void threadBuild3() throws InterruptedException {
        Long start = System.currentTimeMillis();
        ExecutorService executorService = Executors.newCachedThreadPool();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicInteger atomicInteger = new AtomicInteger();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                atomicInteger.set(sum());
                countDownLatch.countDown();
            }
        });
        executorService.shutdown();
        countDownLatch.await();
        System.out.println("异步计算结果为："+atomicInteger.get());
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");

    }
    private static void threadBuild2() throws InterruptedException {
        Long start = System.currentTimeMillis();
        FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return sum();
            }
        });
        new Thread(futureTask).start();
        int result = 0;
        try {
            result = futureTask.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("异步计算结果为："+result);
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
    }
    private static void threadBuild1() throws InterruptedException {

        Long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicInteger atomicInteger = new AtomicInteger();
        Map<String,Integer> result = new HashMap<>();
        new Thread(()->{
            //System.out.println(sum()); //这是得到的返回值
            atomicInteger.set(sum());
            result.put("data",sum());
            countDownLatch.countDown();
        }).start();

        countDownLatch.await();
        System.out.println(atomicInteger.get());
        System.out.println("异步计算结果为："+atomicInteger.get());
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
    }
    private static int sum() {

        return fibo(36);
    }

    private static int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}
